<template>
  <div class="com-container">
    <div class="com-charts" ref="hot_ref"></div>
    <span
      class="iconfont arr-left"
      @click="toLeft"
      :style="comStyle"
    >&#xe6ef;</span>
    <span
      class="iconfont arr-right"
      @click="toRight"
      :style="comStyle"
    >&#xe6ed;</span>
    <span class="cat-name"  :style="comStyle">{{ catName }}</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      echartInstance: null, //echarts实例化对象
      allData: null, //服务器获取的所有数据
      currentIndex: 0,
      titleFontSize: 0,
    };
  },
  computed: {
    catName() {
      if (!this.allData) {
        return "";
      } else {
        return this.allData[this.currentIndex].name;
      }
    },
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px"
      };
    },
  },
  //页面初始化进行的操作
  mounted() {
    this.initecharts();
    this.getData();
    //屏幕大小监听事件
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },

  methods: {
    //1. 初始化echarts
    initecharts() {
      this.echartInstance = this.$echarts.init(this.$refs.hot_ref, "chalk"); //初始化
      const creatOption = {
        //标题
        title: {
          text: "▎热销商品销售金额占比统计",
          left: 20,
          top: 20,
        },
        //图例位置
        legend: {
          top: "15%",
          icon: "circle",
        },

        tooltip: {
          show: true,
          formatter: (arg) => {
            const thirdCategory = arg.data.children;
            //计算所有三级分类的数值总和
            let total = 0;
            thirdCategory.forEach((item) => {
              total += item.value;
            });
            let retStr = "";
            thirdCategory.forEach((item) => {
              retStr += `
              ${item.name}:${parseInt((item.value / total) * 100) + "%"}
              <br/>
              `;
            });
            return retStr;
          },
        },
        series: [
          {
            type: "pie",
            label: {
              show: false,
            },
            emphasis: {
              label: {
                show: true,
              },
              labelLine: {
                show: false,
              },
            },
          },
        ],
      };
      this.echartInstance.setOption(creatOption);
    },
    //获取数据
    async getData() {
      const { data: ret } = await this.$axios.get("hot");
      this.allData = ret;
      console.log(this.allData);
      this.updateecharts();
    },
    //数据处理
    updateecharts() {
      //获取图例数据
      const legendDataArr = this.allData[this.currentIndex].children.map(
        (item) => {
          return item.name;
        }
      );
      //获取一级区域数据
      const seriesDataArr = this.allData[this.currentIndex].children.map(
        (item) => {
          return {
            name: item.name,
            value: item.value,
            children: item.children,
          };
        }
      );
      const dataOption = {
        //图例展示
        legend: {
          data: legendDataArr,
        },
        series: [
          {
            data: seriesDataArr,
          },
        ],
      };
      this.echartInstance.setOption(dataOption);
    },
    //分辨率自适应
    screenAdapter() {
      this.titleFontSize = (this.$refs.hot_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        //标题
        title: {
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
        //图例
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize / 2, //图例标题间隔
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
        series: [
          {
            //饼图大小
            radius: this.titleFontSize * 4.5,
            center: ["50%", "60%"],
          },
        ],
      };
      this.echartInstance.setOption(adapterOption);
      // 手动的调用图表对象的resize 才能产生效果
      this.echartInstance.resize();
    },

    toLeft() {
      this.currentIndex--;
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1;
      }
      this.updateecharts();
    },
    toRight() {
      this.currentIndex++;
      if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0;
      }
      this.updateecharts();
    },
  },
};
</script>

<style lang='less' scoped>
.arr-left {
  position: absolute;
  left: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}
.arr-right {
  position: absolute;
  right: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}
.cat-name {
  position: absolute;
  left: 80%;
  bottom: 20px;
  color: white;
}
</style>
